package com.sd.repay.transfer.bank;

import com.sd.repay.common.CommonResult;
import com.sd.repay.pojo.TransferOrder;
import com.sd.repay.transfer.TransferService;
import com.sd.repay.utils.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;

import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * 代付抽象类
 *
 * @author
 */
public abstract class BaseRepayService {

    @Autowired
    public TransferService transferService;

    /**
     * 订单号前缀
     */
    public final String orderTitle = "ZY";

    /**
     * 编码格式
     **/
    public final String enCode = "UTF-8";

    /**
     * 超时时间
     **/
    public final String timeOut = "40000";

    /**
     * 一百
     */
    public BigDecimal oneHundred = new BigDecimal("100");

    /**
     * 时间格式化
     */
    public SimpleDateFormat format = new SimpleDateFormat("yyyyMMdd");


    /**
     * 代付订单号
     * <p>
     * <p>
     * 规则:共26位 订单前缀(2位)+年月日(yyyyMMdd 8位)+订单ID(11位订单ID，不够11位左补0)+0(1位)+身份证号(14 ~ 17 4位 )
     *
     * @param order
     * @return
     */
    public synchronized String getOrderNo(TransferOrder order) {
        Date date = new Date();
        String today = format.format(date);
        String idCard = order.getIdentityNo();
        String orderId = String.valueOf(order.getId());
        String idCardSubfix = idCard.substring((idCard.length() - 5), (idCard.length() - 1));
        String orderIdLeftZero = StringUtil.stringFillLeftZero(orderId, 11);
        return orderTitle.concat(today).concat(orderIdLeftZero).concat("0" + idCardSubfix);
    }


    /**
     * 通道校验
     * <p>
     * <p>
     * 提交代付前校验
     *
     * @param order
     * @return
     */
    public abstract CommonResult channelCheck(TransferOrder order);

    /**
     * 单笔代付
     *
     * @param order 出款明细
     * @return
     */
    public abstract void transferOrder(TransferOrder order);


    /**
     * 单笔代付结果查询
     *
     * @param transferOrder
     */
    public abstract void queryTransferStatus(TransferOrder order);


}
